package com.chris.homecare.components.renderers
{
	import com.chris.homecare.components.NameValuePairLabel;
	import com.chris.homecare.data.EventData;
	import com.chris.homecare.utils.CommonUtils;
	
	import feathers.controls.ImageLoader;
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.controls.List;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.layout.AnchorLayout;
	import feathers.layout.AnchorLayoutData;
	
	import flash.geom.Point;
	
	import starling.display.Quad;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	[Event(name="showDetailEvent",type="starling.events.Event")]
	public class EventListRenderer extends LayoutGroup implements IListItemRenderer
	{
		public static const SHOW_DETAIL_EVENT:String = "showDetailEvent";
		
		protected var _data:Object;
		protected var _index:int = -1;
		protected var _owner:List;
		private var _isSelected:Boolean = false;
		private var selectedValue:int = 0;
		
		private var monthsArray:Array = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
		
		protected var _touchPointID:int = -1;
		private static const HELPER_TOUCHES_VECTOR:Vector.<Touch> = new <Touch>[];
		private static const HELPER_POINT:Point = new Point();
		
		private var background:Quad;
		private var bottomBorderQuad:Quad;
		private var rightAccessory:ImageLoader;
		private var topBorderQuad:Quad;

		private var headingLabel:Label;

		private var orgainiserComp:NameValuePairLabel;
		private var locationGroup:NameValuePairLabel;

		private var dateLabel:Label;
		
		public function EventListRenderer()
		{
			this.addEventListener(TouchEvent.TOUCH, touchHandler);
		}
		
		public function get data():Object
		{
			return this._data;
		}
		
		public function set data(value:Object):void
		{
			if(this._data == value)
			{
				return;
			}
			this._data = value;
			this.invalidate(INVALIDATION_FLAG_DATA);
		}
		
		public function get index():int
		{
			return this._index;
		}
		
		public function set index(value:int):void
		{
			this._index = value;
		}
		
		public function get owner():List
		{
			return List(this._owner);
		}
		
		public function set owner(value:List):void
		{
			if(this._owner == value)
			{
				return;
			}
			
			if(this._owner)
			{
				this._owner.removeEventListener(Event.SCROLL, owner_scrollHandler);
			}
			
			this._owner = value;
			
			if(this._owner)
			{
				this._owner.addEventListener(Event.SCROLL, owner_scrollHandler);
			}
			
			this.invalidate(INVALIDATION_FLAG_DATA);
		}
		
		public function get isSelected():Boolean
		{
			return this._isSelected;
		}
		
		public function set isSelected(value:Boolean):void
		{
			if(this.isSelected == value)
			{
				return;
			}
			
			this._isSelected = value;
			this.invalidate(INVALIDATION_FLAG_SELECTED);
			this.dispatchEventWith(Event.CHANGE);
		}
		
		protected function owner_scrollHandler(event:Event):void
		{
			this._touchPointID = -1;
		}
		
		override protected function initialize():void
		{
			this.layout = new AnchorLayout();
			
			background = new Quad(2, 2, 0xffffff);
			bottomBorderQuad = new Quad(10, 1, 0xe6e6e6);
			topBorderQuad = new Quad(10, 1, 0xe6e6e6);
			
			addChild(background);
			addChild(topBorderQuad);
			addChild(bottomBorderQuad);
			
			headingLabel = new Label();
			headingLabel.x = headingLabel.y = 10;
			headingLabel.nameList.add("event-heading-lbl");
			addChild(headingLabel);
			

			/*organiserGroup = new NameValuePairLabel("Organiser", eventData.organiser);
			dateGroup = new NameValuePairLabel("Date", eventData.date.toDateString());
			locationGroup = new NameValuePairLabel("Location", eventData.location);
			
			organiserGroup.x = dateGroup.x = locationGroup.x = 15;
			
			addChild(organiserGroup);
			addChild(dateGroup);
			addChild(locationGroup);*/
			
			orgainiserComp = new NameValuePairLabel("","");
			orgainiserComp.x = 10;
			orgainiserComp.y = 60*CommonUtils.scaleValue;
			addChild(orgainiserComp);
			
			locationGroup = new NameValuePairLabel("","");
			locationGroup.x = 10;
			locationGroup.y = 90*CommonUtils.scaleValue
			addChild(locationGroup);
			
			dateLabel = new Label();
			var dateLableLayoutData:AnchorLayoutData = new AnchorLayoutData();
			dateLableLayoutData.bottom = 5;
			dateLableLayoutData.right = 5;
			dateLabel.layoutData = dateLableLayoutData;
			dateLabel.nameList.add("review-list-title-lbl");
			addChild(dateLabel);
			
			var rendererHeight:int = 140*CommonUtils.scaleValue;
			this.height= background.height = bottomBorderQuad.y = rendererHeight;
			
			super.initialize();
		}
		
		override protected function draw():void
		{
			commitData();
			commitLayout();
			super.draw();
		}
		
		private function commitData():void
		{
			if(this._owner && this._data)
			{
				var dataObj:EventData = this._data as EventData;
				headingLabel.text = dataObj.titel;
				orgainiserComp.setNameValue("Time",dataObj.organiser); 
				locationGroup.setNameValue("Location",dataObj.location);
				dateLabel.text = dataObj.date.date +"-"+ monthsArray[dataObj.date.month] +"-"+ dataObj.date.fullYear;
			}
				
			else
			{
				headingLabel.text = "";
			}
		}
		
		protected function commitLayout():void
		{
			topBorderQuad.width = background.width = bottomBorderQuad.width = this.width;
		}
		
		protected function touchHandler(event:TouchEvent):void
		{
			if(!this._isEnabled)
			{
				return;
			}
			
			const touches:Vector.<Touch> = event.getTouches(this, null, HELPER_TOUCHES_VECTOR);
			if(touches.length == 0)
			{
				//end of hover
				return;
			}
			if(this._touchPointID >= 0)
			{
				var touch:Touch;
				for each(var currentTouch:Touch in touches)
				{
					if(currentTouch.id == this._touchPointID)
					{
						touch = currentTouch;
						break;
					}
				}
				
				if(!touch)
				{
					//end of hover
					HELPER_TOUCHES_VECTOR.length = 0;
					return;
				}
				
				if(touch.phase == TouchPhase.ENDED)
				{
					this._touchPointID = -1;
					touch.getLocation(this, HELPER_POINT);
					var isInBounds:Boolean = this.hitTest(HELPER_POINT, true) != null;
					
					if(isInBounds)
					{
						this.isSelected = !this.isSelected;
						this.dispatchEventWith(SHOW_DETAIL_EVENT, true, this._data);
					}
				}
			}
			else //if we get here, we don't have a saved touch ID yet
			{
				for each(touch in touches)
				{
					if(touch.phase == TouchPhase.BEGAN)
					{
						this._touchPointID = touch.id;
						break;
					}
				}
			}
			HELPER_TOUCHES_VECTOR.length = 0;
		}
	}
}